package com.hyl.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hyl.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016101800712422";

    /// 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdeBlB8iP4IBErS/v4lD+lGyaEFvvg3qmhQcPpbT+gW1XdJnyaRiobe+RKAoGxnSRwa/pw5WgXTHO3hN0vKR8IGWzeqNJzi7a5NXl8KThnHiSjaCL15IFxLcwJxC+ZHfOJlvP5YRuU4x2o6iRDmgBRMAI6ypkIxXKk+fvO5PdWlqe0+DdrkwKJZWI2N+Onj8hYgFTfQJ37ZUMqSE/6E55/pC3KkvM5IGv/mp9FmdS5dfBe5Kjnd4OBB5BEJWRwrttsT1cMN4PGDf+wiCDjCn8PdxJ2lsIdr4O6KXB7vUSi1ZNrCBrYRAQfsdi6Fp1xb3zHoThX846ydD5wqzCmaOOnAgMBAAECggEAcCh1nQHx2q5EbFlPnAtDlb7T3StdthoCKkhkWSOmOPo1GzJpd5zYKwFAZfKJG8/4f1Z5Z+YOEOI0XZd1zzxzzNoHMESAuSnfOOW8pNHbSZ8vq2/lzsw14j4jYSVqXZ32s55gIdSnx5FIs+IpeDj9VtZiECvzekrgUF95mjB9dx7xtebJracYi85MvBlOOW4f/Lfk9twE5yWM2xrDhfG12Ts76bnVSjZDHJxkZkQEGv1JmsndmT7wDJi8lg5OkeF0Ft0iCjqLJK/CXWa7JO428o+YC/perGDHjp2jewrDzoS2ZzuEFLTehXbGf8Oth4G2rYirsHjEa44gt2gICPU5uQKBgQDfCF9wdUCfZaiBVFBa0I9XwtkmvKEPZyIa3CSCTjXDYkorw+xOijQleMqN6tORqlQd/W3Co/6KZ1BiAGtLUUoV+BWgiHyONl0/iPiBRTZuWe3rypoVVp6s8CGCnNlsNh/VCWMVKakss8tkB92cFzTIGJk3IT1+4Y8e4jLsuzKRdQKBgQC0vsel5ShFwsx9Oy1UvDyRJUDxvQcAoQb3tgfGfnpB36c1iM92bFt+ntCLfE2P7TF+GR5ydnOKwQ14GbDN6ncBs9B0OALnvsMtbXlSTRvngc+IZI7C3TbYZ6XN3ymzBqDjL8bR65tKOi2hfoK06tCtcAl+x9mI24EKF/pnjNUBKwKBgBGQTjvD8A/daJSuuBk+vVsS7Bjh1+knnQbyeSmN0SwOzHp/1lclCRA+X+0nJfwdprwp3f/q/DuYATuGfuySfTwXqlRXC0ADxfof5jn/4lZ0XIkqDlJJ3PsPegQYKAMejAkx0Qy0mC7+BbFd8TS+mxh63234/yfDyYqME6iS4VuVAoGBAKfv/5bvxiyTY+cT/fmWcPw3zhEOnpwCAnRU6QKQb84XHcjLFmClK5oJvCFuXK7XJ75PXo/h6VdKKtaloDajnPGy/oONq31iLP1fk3NZ/Axt+8NyE4eRfS0zmMYhjSAvj3un36casA4W4mEEKDQrXbtYtynaTRAxS8NyJhLisuNDAoGARmeM0kiOHmQdEXff0dyqCXdcCp5VvP1aavNUzmsylzE1KdHh5bwO+vWRRT0DtwmLxIpZtZrAOzMXVSipKLiQ/ALLYvloZQ8ToeamO6hORFBjlfhNFEoewEGU6cPsSsA6O27GjCThSoMqgFr8M9joV6bNk+/WFJv4g8OR9P3w+GY=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiyUJgwF/dWFiKStzezY+Iu798t9m8fvVG51XITx3rjjWu5I2bJQpHpSmkyg4Y0OZ1WsQ/fsnm0QS2hob3hylfIhp40HgG9mJmAUUEk3HaSkS6ThERxXdw/9Ku+4MflICA6ZeXpD7Pk5r5P11pj39es8ywLldkNAsrqy26QjnPPWDrRcz7KCdB+orRqVhqDoWw7kJ4FYFxwqvT8vOe9YF/a4uD9+DwfeCxm3rHC7EKtKIbsoaopiaE3puAid0JLCZJ/ziFrUIyQiVPxzNFir2J7jtow88ReTK0HIZdYq9J4yq6ZAxdCP6jCOLaLPqjfZAeBh2AnQ3+DMjuwlZ4j9mNQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://urcbye.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://www.member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout ="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
